var { ReturnModel } = require('../libs/resmodel');
var {
    getToken,
    getOpenid,
    decodeDataInfo,
    mybuildqrcode,
    mybuildreport,
    subMsg_callback,
    getMsgTmpList,
    msgIsNeedRead,
    sendMsg_hand,
    peopleRecommend,
    wx_sub_callback,
    wx_sub_callback_val,
    First,
    getUserOfficial
} = require('../services/wechat');
var { init_auth }= require('../libs/sms')

/**
 * 获取签名
 * @api {post} /wechat/getAccessToken 获取微信签名AccessToken
 * @apiDescription 获取签名
 * @apiName getAccessToken
 * @apiSampleRequest /wechat/getAccessToken
 * @apiGroup wechat-微信
 * @apiVersion 1.0.0
 * @apiSuccessExample {json} Success-Response:
 *   HTTP/1.1 200 OK
 *   {
 *      "data": "55_p6Mw5tV1YWHj4QNZAh8CtwybiSkUWfkwHkw7zhaUDS7Ab7JVcxR5dzl-HM90YeKbAnBbofqmFW8mX79PPVimbS0WugCqyzdAkLyrT7QS3FjW_6wmr6qH2-kNyAwM67UwJWq1MMd3lBmuMtx1ERFbABAANB",
 *      "msg": "获取成功",
 *      "code": 0
 *   }
 */
async function getAccessToken(req, res) {
    let result = await getToken(req.query.name || req.body.name);
    res.json(new ReturnModel(result));
}
/**
* 获取用户唯一code
* @api {post} /wechat/getPeopleCode 获取用户唯一pcode
* @apiDescription 获取用户唯一pcode
* @apiName getOpenidByCode
* @apiParam (参数) {String} appcode 用户登录小程序pcode
* @apiParam (参数) {String} ycode 邀请人用户code
* @apiSampleRequest /wechat/getPeopleCode
* @apiGroup wechat-微信
* @apiVersion 1.0.0
* @apiParamExample {json} Request-Example:
*     {
*       "appcode": "asdasd1231sdsd",
*       "ycode": "13912393219"
*     }
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "data": {
*            "pcode": "290464549598", // 用户个人系统编号
*            "isfirst": 1, //1：第一次登录未选择身份， 0：已经保存过身份
*            "isOfficial": 1,  //1：已关注公众号， 0：未关注公众号
*            "avator": "xxxxxx", //用户头像
*            "nickname": "会飞的鱼", //用户昵称
*        },
*      "msg": "获取成功",
*      "code": 0
*   }
*/
async function getPeopleCode(req, res) {
    let result = await getOpenid(req.body);
    res.json(new ReturnModel(result));
}
/**
 * 解密数据
 * @api {post} /wechat/decodeData 解密数据
 * @apiDescription 解密数据
 * @apiName decodeData
 * @apiParam (参数) {String} pcode 用户code
 * @apiParam (参数) {String} encryptedData 待解密数据
 * @apiParam (参数) {String} iv 解密向量
 * @apiSampleRequest /wechat/decodeData
 * @apiGroup wechat-微信
 * @apiVersion 1.0.0
 * @apiParamExample {json} Request-Example:
 *     {
 *       "pcode": "290464549598",
 *       "encryptedData": "aaaaaaaaaaa123asdasdada1223123",
 *       "iv": "aaaaaa",
 *     }
 * @apiSuccessExample {json} Success-Response:
 *   HTTP/1.1 200 OK
 *   {
 *      "data": "p6Mw5tV1YWHj4QNZAh8CtwybiSkUWfkwHkw7zh",
 *      "msg": "解密成功",
 *      "code": 0
 *   }
 */
async function decodeData(req, res) {
    let pcode = req.body.pcode;
    let encryptedData = req.body.encryptedData;
    let iv = req.body.iv;
    let result = await decodeDataInfo(pcode, encryptedData, iv);
    res.json(new ReturnModel(result));
}
/**
 * 获取小程序二维码
 * @api {post} /wechat/buildqrcode 获取小程序二维码
 * @apiDescription 获取小程序二维码
 * @apiName buildqrcode
 * @apiParam (参数) {String} pcode  用户code
 * @apiSampleRequest /wechat/buildqrcode
 * @apiGroup wechat-微信
 * @apiVersion 1.0.0
 * * @apiParamExample {json} Request-Example:
*     {
*       "pcode": "423829323253"
*     }
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "data": "https://redspider.oss-cn-shenzhen.aliyuncs.com/redspider_v1.0/173198057683_423829323253.png",
*      "msg": "获取成功",
*      "code": 0
*   }
*/
async function buildqrcode(req, res) {
    let result = await mybuildqrcode(req.body.pcode);
    res.json(new ReturnModel(result));
}
/**
 * 获取推荐人海报
 * @api {post} /wechat/buildreport 获取推荐人海报
 * @apiDescription 获取推荐人海报
 * @apiName buildreport
 * @apiParam (参数) {String} pcode  用户code
 * @apiSampleRequest /wechat/buildreport
 * @apiGroup wechat-微信
 * @apiVersion 1.0.0
 * * @apiParamExample {json} Request-Example:
*     {
*       "pcode": "423829323253"
*     }
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "data": "https://redspider.oss-cn-shenzhen.aliyuncs.com/redspider_v1.0/173198057683_423829323253.png",
*      "msg": "获取成功",
*      "code": 0
*   }
*/
async function buildreport(req, res) {
    let result = await mybuildreport(req.body.pcode);
    res.json(new ReturnModel(result));
}
/**
* 订阅消息成功回调函数
* @api {post} /wechat/subMsg_callback 订阅消息成功回调函数
* @apiDescription 后台保存消息订阅状态，用于后续是否需要再次订阅
* @apiName subMsg_callback
* @apiParam (参数) {String} pcode  用户code
* @apiParam (参数) {String} errMsg  接口调用成功时errMsg值为'requestSubscribeMessage:ok'
* @apiParam (参数) {String} TEMPLATE_ID:String  [TEMPLATE_ID]是动态的键，即模板id，值包括'accept'、'reject'、'ban'、'filter'。'accept'表示用户同意订阅该条id对应的模板消息，'reject'表示用户拒绝订阅该条id对应的模板消息，'ban'表示已被后台封禁，'filter'表示该模板因为模板标题同名被后台过滤
* @apiSampleRequest /wechat/subMsg_callback
* @apiGroup wechat-微信
* @apiVersion 1.0.0
* * @apiParamExample {json} Request-Example:
*     {
*       "pcode": "29046454959", 
*       "errMsg": "requestSubscribeMessage:ok", 
*       "zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE": "accept"
*     }
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "msg": "订阅状态保存成功",
*      "code": 0
*   }
*/
async function requestSubscribeMessage_callback(req,res){
    let result = await subMsg_callback(req.body);
    res.json(new ReturnModel(result));
}
/**
* 获取消息模板列表
* @api {get} /wechat/getMessageTmpList 获取消息模板列表
* @apiDescription 获取消息模板列表
* @apiName getMessageTmpList
* @apiSampleRequest /wechat/getMessageTmpList
* @apiGroup wechat-微信
* @apiVersion 1.0.0
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "msg": "获取成功",
*      "code": 0
*      "data": [
*        {
*            "priTmplId": "4FXJgETPqzTsjNTX4PQ9tETgkwcT9hCnhqr5kF1FxuE",
*            "title": "审核结果通知",
*            "content": "备注:{{thing2.DATA}}\n时间:{{time1.DATA}}\n",
*            "example": "备注:恭喜通过\n时间:2021年11月20日\n",
*            "type": 2,
*            "keywordEnumValueList": []
*        }
*       ],
*   }
*/
async function getMessageTmpList(req,res){
    let result = await getMsgTmpList();
    res.json(new ReturnModel(result));
}
/**
* 查询用户是否需要订阅消息
* @api {get} /wechat/msgIsRead 查询用户是否需要订阅消息
* @apiDescription 用户未订阅过消息、订阅一次性消息已被发送用掉需要再次弹窗提示订阅
* @apiName msgIsRead
* @apiParam (参数) {String} pcode  用户code
* @apiSampleRequest /wechat/msgIsRead
* @apiGroup wechat-微信
* @apiVersion 1.0.0
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "msg": "获取成功",
*      "code": 0,
*      "data": true, // true 需要订阅消息，false 已订阅，不需要重新订阅
*   }
*/
async function msgIsRead(req,res){
    let result = await msgIsNeedRead(req.body.pcode);
    res.json(new ReturnModel(result));
}
/**
* 给指定人发送推荐消息-订阅消息（后台）
* @api {post} /wechat/sendMsg  给指定人发送推荐消息-订阅消息（后台）
* @apiDescription  给指定人发送推荐消息-订阅消息（后台）
* @apiName sendMsg
* @apiParam (参数) {String} pcode  用户code
* @apiParam (参数) {String} content  消息内容
* @apiParam (参数) {String} remarks  消息备注
* @apiSampleRequest /wechat/sendMsg
* @apiGroup wechat-微信
* @apiVersion 1.0.0
* @apiParamExample {json} Request-Example:
*     {
*       "pcode": "290464549598"，
*       "content": "您的个人资料尚未填写，请前去填写完整，平台将第一时间将您推荐给合适的对象！"，
*       "remarks": "前往小程序首页查看详情，感谢您的支持！"，
*     }
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "msg": "发送成功",
*      "code": 0
*   }
*/
async function sendMsg(req,res){
    let result = await sendMsg_hand(req.body);
    res.json(new ReturnModel(result));
}
/**
* 推荐某人-后台
* @api {post} /wechat/recommend 推荐某人-后台
* @apiDescription 后台可以主动推荐某人，则某人优先显示在小程序首页，并给所有用户发送订阅消息
* @apiName recommend
* @apiParam (参数) {String} pcode 用户code
* @apiSampleRequest /wechat/recommend
* @apiGroup wechat-微信
* @apiVersion 1.0.0
* @apiParamExample {json} Request-Example:
*     {
*       "pcode": "290464549598"
*     }
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "msg": "推荐成功",
*      "code": 0
*   }
*/
async function recommend(req, res) {
    let result = await peopleRecommend(req.body);
    res.json(new ReturnModel(result));
}
async function initAuth(req, res) {
    let result = await init_auth();
    res.json(new ReturnModel(result));
}
async function wxSubCallBackVal(req, res) {
    let result = await wx_sub_callback_val(req);
    res.send(result);
}
async function wxSubCallBack(req, res) {
    let result = await wx_sub_callback(req);
    res.json(result);
}
/**
* 获取用户是否第一次登陆
* @api {post} /wechat/first 获取用户是否第一次登陆
* @apiDescription 获取用户是否第一次登陆
* @apiName first
* @apiParam (参数) {String} pcode 用户code
* @apiSampleRequest /wechat/first
* @apiGroup wechat-微信
* @apiVersion 1.0.0
* @apiParamExample {json} Request-Example:
*    {
*     pcode: 12321231598,
*    }
* @apiSuccessExample {json} Success-Response:
*   HTTP/1.1 200 OK
*   {
*      "data": {
*            "pcode": "290464549598", // 用户个人系统编号
*            "isfirst": 1 //1：第一次登录未选择身份， 0：已经保存过身份
*            "isOfficial": 1 //1：已关注公众号  0：未关注公众号
*            "avator": 1 //用户头像
*            "nickname": 1 //用户昵称
*        },
*      "msg": "获取成功",
*      "code": 0
*   }
*/
async function first(req, res){
    let result = await First(req.body.pcode);
    res.json(new ReturnModel(result));
}

module.exports = function (app) {
    let base_url = '/api/wechat';

    app.post(base_url + '/getAccessToken', getAccessToken);
    app.get(base_url + '/getAccessToken', getAccessToken);
    app.post(base_url + '/getPeopleCode', getPeopleCode);
    app.post(base_url + '/decodeData', decodeData);
    app.post(base_url + '/buildqrcode', buildqrcode);
    app.post(base_url + '/buildreport', buildreport);
    app.get(base_url + '/getMessageTmpList', getMessageTmpList);
    app.post(base_url + '/subMsg_callback', requestSubscribeMessage_callback);
    app.post(base_url + '/msgIsRead', msgIsRead);
    app.post(base_url + '/sendMsg', sendMsg);
    app.post(base_url+'/recommend', recommend);
    app.post(base_url+'/init_auth', initAuth);
    app.post(base_url+'/first', first);
    app.post(base_url+'/getUserOfficial', getUserOfficial);
    app.get(base_url+'/wxSubCallBack', wxSubCallBackVal);
    app.post(base_url+'/wxSubCallBack', wxSubCallBack);
}
